var pageTips=getUrlVal("pageTips");//页面跳转标识1从商品订单过去，2从购物车订单过去
var userMess="";
$(function(){
    //获取用户信息
   getUser();
    userMess=JSON.stringify(ym.user);
   getAddrLi()
});
function getAddrLi(){
    $(".addrUl").html("");
    ym.ajax({
        url:'/shop/address/getAddress',
        param:{
            ymUser:userMess,
        },
        success:function(data){
            console.log(data);
            console.log("获取地址列表")
            var html="";
            if(data.obj!=null&&data.obj.length>0){
                $(".addrUl").removeClass("hide");
                $(".noAddr").addClass("hide");
                for(var i=0;i<data.obj.length;i++){
                    var l=data.obj[i];
                    html+='<li>'
                    html+='<span onclick="sureAddr('+l.receivingId+')"><span class="contacts">收货人：'+l.receiverName+'</span>'
                    var detailedAddress=(l.detailedAddress).replace(/,/g, "");
                    html+='<span class="contAddr">收货地址：'+detailedAddress+' '+l.receiverAddress+'</span></span>'
                    html+='<span class="addrOperation">'
                    html+='<span class="ar-left">'
                    html+='<span class="radio_box">'
                    if(l.isDefault=="Y"){
                        html+='<input type="radio" id="radio_'+i+'" name="radio" checked onclick="setIsDefault('+l.receivingId+')">'
                        html+='<label for="radio_'+i+'" class="choiceArr"></label>'
                    }else{
                        html+='<input type="radio" id="radio_'+i+'" name="radio" onclick="setIsDefault('+l.receivingId+')">'
                        html+='<label for="radio_'+i+'" class="choiceArr"></label>'
                    }
                    html+='<span class="choiceTitle">默认地址</span>'
                    html+='</span></span>'
                    html+='<span class="ar-right">'
                    html+='<span class="editArr" onclick="editAddr('+l.receivingId+')">编辑</span>'
                    html+='<span class="delArr" onclick="delAddr('+l.receivingId+')">删除</span>'
                    html+='</span></span></li>'
                }
                $(".addrUl").append(html);
            }else{
                $(".noAddr").removeClass("hide");
                $(".addrUl").addClass("hide");
            }
        }
    });
}
//编辑地址
function editAddr(id){
    window.location.href="editAddr.html?receivingId="+id+"&pageTips="+pageTips;
}
//设置默认地址
function setIsDefault(id){
    ym.ajax({
        url : "/shop/address/update",
        type : "post",
        param:{
            receivingId:id,
            isDefault:"Y",
            ymUser:userMess,
        },
        success : function(data) {

        }
    })
}
//删除地址
function delAddr(id){
    $('#myConfirm .am-modal-bd').html("您是否确定删除此地址？")
    var $confirm = $('#myConfirm');
    var confirm = $confirm.data('amui.modal');
    var onConfirm = function() {
        //点击确认按钮调用事件
        ym.ajax({
            url:'/shop/address/del',
            param:{
                receivingId:id,
            },
            success:function(data){
                console.log(data)
                if(data.res==1){
                    $("#alertModal .am-modal-bd").html("删除成功！");
                    $("#alertModal").modal();
                    getAddrLi();
                    setTime();
                }
            }
        });

    };
    var onCancel = function() {
        //取消按钮调用事件
    };
    if (confirm) {
        confirm.options.onConfirm =  onConfirm;
        confirm.options.onCancel =  onCancel;
        confirm.toggle(this);
    } else {//重定义模态窗函数
        $confirm.modal({
            relatedElement: this,
            closeViaDimmer: false,//禁止点击遮罩层关闭
            onConfirm: onConfirm,
            onCancel: onCancel
        });
    }
}
/*两秒后关闭弹窗*/
function setTime(){
    setTimeout(function () {
        $("#alertModal").modal('close');
    }, 2000);
}
/*跳转新增地址*/
function addAddr(){
    window.location.href="editAddr.html?pageTips="+pageTips;
}
/*确定选择地址*/
function sureAddr(id){
   if(pageTips==1){
       window.location.href="sureOrderDetail.html?addrId="+id;
   }else if(pageTips==2){
       window.location.href="sureOrder.html?addrId="+id;
   }else{
       window.location.href="person.html";
   }
}
